﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace BingDesktopChanger
{
    internal class WallPaperChanger
    {

        const int SPI_SETDESKWALLPAPER = 20;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        public enum Style : int
        {
            Tiled,
            Centered,
            Stretched
        }
        public static void Set(Uri uri, Style style)
        {
            Stream s = new WebClient().OpenRead(uri.ToString());
            Image img = Image.FromStream(s);
            //Image img = ScaleImage(Image.FromStream(s));
            //style = Style.Centered;
            string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
            img.Save(tempPath, ImageFormat.Bmp);

            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
            switch (style)
            {
                case Style.Tiled:
                    key.SetValue(@"WallpaperStyle", 1.ToString());
                    key.SetValue(@"TileWallpaper", 1.ToString());
                    break;
                case Style.Centered:
                    key.SetValue(@"WallpaperStyle", 1.ToString());
                    key.SetValue(@"TileWallpaper", 0.ToString());
                    break;
                case Style.Stretched:
                    key.SetValue(@"WallpaperStyle", 2.ToString());
                    key.SetValue(@"TileWallpaper", 0.ToString());
                    break;
                default:
                    break;
            }
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
        }

        private static Image ScaleImage(Image image)
        {
            Rectangle screenSize = Screen.PrimaryScreen.WorkingArea;
            Size size = new Size(image.Width, image.Height);
            while (size.Width < screenSize.Width || size.Height < screenSize.Height)
            {
                size.Width += Convert.ToInt32(size.Width + .1);
                size.Height += Convert.ToInt32(size.Height + .1); ;
            }
            return ResizeImage(image, size);
        }


        private static Image ResizeImage(Image input, Size newSize)
        {
            Bitmap newImg = new Bitmap(newSize.Width, newSize.Height);
            using (Graphics g = Graphics.FromImage(newImg))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(input, 0, 0, newSize.Width, newSize.Height);
            }
            return (Image)newImg;
        }
    }
}
